#include <Dynamics/Boundary/fixed_freedom_cpu.h>

namespace PhysLeo {

template<typename T>
void FixedFreedomCpu<T>::allocateResource(SimData<T>& simData)
{
    ptr_fix_ = simData.ptr_fix_;
}

template<typename T>
void FixedFreedomCpu<T>::clearFixedFreedom(std::shared_ptr<BufferData<glm::tvec3<T>>> vec)
{
    const auto fixed_ptr = ptr_fix_->ptrCpu();
    auto vec_ptr = reinterpret_cast<T*>(vec->ptrCpu());
    const auto fixed_size = ptr_fix_->size();
    for(auto i=0;i<fixed_size;++i)
    {
        auto index = fixed_ptr[i];
        vec_ptr[index] = 0.0f;
    }
}

template<typename T>
void FixedFreedomCpu<T>::setFixedFreedom(std::shared_ptr<BufferData<glm::tvec3<T>>> vec, std::shared_ptr<BufferData<T>> value)
{
    const auto fixed_ptr = ptr_fix_->ptrCpu();
    auto vec_ptr = reinterpret_cast<T*>(vec->ptrCpu());
    auto value_ptr = value->ptrCpu();
    const auto fixed_size = ptr_fix_->size();
    for (auto i = 0; i<fixed_size; ++i)
    {
        auto index = fixed_ptr[i];
        vec_ptr[index] = value_ptr[i];
    }
}

template class FixedFreedomCpu<float>;
template class FixedFreedomCpu<double>;

}